package com.blog.web.controller;

import com.blog.common.core.domain.R;
import com.blog.common.core.web.domain.AjaxResult;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.dto.CommentDto;
import com.blog.common.domain.vo.CommentVo;
import com.blog.common.domain.vo.ReplyVo;
import com.blog.web.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 评论控制器
 * </p>
 *
 * @author Valerie
 * @date 2023/2/27
 **/
@RestController
@Slf4j
@Api(tags = "评论控制器")
public class CommentController {

    @Resource
    private CommentService commentService;



    /**
     * 查询评论
     * @param
     * @return {@link R<TableDataInfo<CommentVo>>} message
     */
    @GetMapping("/comments")
    @ApiOperation("查询评论")
    public R<TableDataInfo<CommentVo>> listComments(CommentDto commentDto){
        return R.ok(commentService.listComments(commentDto));
    }

    /**
     * 添加评论
     *
     * @param commentDto 评论信息
     * @return {@link R<Void>}
     */
    @ApiOperation(value = "添加评论")
    @PostMapping("/comments")
    public R<Void> saveComment(@Valid @RequestBody CommentDto commentDto) {
        commentService.saveComment(commentDto);
        return R.ok();
    }


    /**
     * 评论点赞
     *
     * @param commentId 评论id
     * @return {@link R<Void>}
     */
    @ApiOperation(value = "评论点赞")
    @PostMapping("/comments/{commentId}/like")
    public R<Void> saveCommentLike(@PathVariable("commentId") Serializable commentId) {
        commentService.saveCommentLike(commentId);
        return R.ok();
    }

    /**
     * 查询评论下的回复
     *
     * @param commentId 评论id
     * @return {@link R<List<ReplyVo>>} 回复列表
     */
    @ApiOperation(value = "查询评论下的回复")
    @ApiImplicitParam(name = "commentId", value = "评论id", required = true, dataType = "Integer")
    @GetMapping("/comments/{commentId}/replies")
    public R<List<ReplyVo>> listRepliesByCommentId(@PathVariable("commentId") Serializable commentId) {
        return R.ok(commentService.listRepliesByCommentId(commentId));
    }
}
